﻿using LyData.Data;
using System;
using System.Data;
using System.Web;

public class ManagerLogin
{
    public static bool Login(string managerName, string password)
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataRow row = db.TopRow("Password,ManagerName,PenName,Lock,AddTime,UpTime,LoginTime,LastLoginTime,LoginIP,LastLoginIP,LoginCount,RoleId,loginUrl", "manager", string.Format("ManagerName='{0}' AND Password='{1}'", managerName, Function.MD5(password)), "");
        if (row == null)
        {
            string[,] strArray = new string[,] { { "Info", "登陆失败", "NvarChar", "0" }, { "Url", Function.GetURL(), "NvarChar", "0" }, { "ManagerName", managerName, "NVarChar", "32" }, { "AddIp", Function.GetIP(), "NVarChar", "32" }, { "AddTime", DateTime.Now.ToString(), "DateTime", "0" } };
            db.Insert("log", strArray);
            db.Close();
            return false;
        }
        HttpContext.Current.Session["managerLogin"] = true;
        HttpContext.Current.Session["managerName"] = managerName;
        HttpContext.Current.Session["penName"] = row["PenName"];
        HttpContext.Current.Session["password"] = row["Password"];
        HttpContext.Current.Session["loginUrl"] = row["loginUrl"];
        string str = "";
        int num = 0;
        string str2 = row["RoleId"].ToString().Trim(new char[] { ',' });
        DataTable table = db.DataTable("RoleList,IsAdmin", "managerRole", string.Format("RoleId IN ({0})", str2), "");
        for (int i = 0; i < table.Rows.Count; i++)
        {
            str = str + table.Rows[i][0].ToString();
            num += Convert.ToInt32(table.Rows[i][1].ToString());
        }
        HttpContext.Current.Session["RoleList"] = str;
        HttpContext.Current.Session["IsAdmin"] = num;
        row["lastLoginTime"] = row["loginTime"];
        row["loginTime"] = DateTime.Now.ToString();
        row["lastLoginIP"] = row["loginIP"];
        row["loginIP"] = Function.GetIP();
        row["LoginCount"] = Convert.ToString((int) (Convert.ToInt32(row["LoginCount"]) + 1));
        SiteInfo.SetSiteLanguage("0");
        string[,] includeColumns = new string[,] { { "LoginTime", row["LoginTime"].ToString(), "DateTime", "0" }, { "LastLoginTime", row["LastLoginTime"].ToString(), "DateTime", "0" }, { "LoginIP", row["loginIP"].ToString(), "NVarChar", "32" }, { "LastLoginIP", row["lastLoginIP"].ToString(), "NVarChar", "32" }, { "LoginCount", row["LoginCount"].ToString(), "int", "0" } };
        db.Update("manager", includeColumns, string.Format("ManagerName='{0}'", managerName));
        string[,] strArray3 = new string[,] { { "Info", "登陆成功", "NvarChar", "0" }, { "Url", Function.GetURL(), "NvarChar", "0" }, { "ManagerName", managerName, "NVarChar", "32" }, { "AddIp", Function.GetIP(), "NVarChar", "32" }, { "AddTime", DateTime.Now.ToString(), "DateTime", "0" } };
        db.Insert("log", strArray3);
        db.Close();
        return true;
    }

    public static void Out()
    {
        HttpContext.Current.Session.Clear();
        HttpContext.Current.Server.Transfer("~/Admin/Login.aspx");
    }

    public static bool Validate()
    {
        if (HttpContext.Current.Session["managerLogin"] == null)
        {
            return false;
        }
        return (bool) HttpContext.Current.Session["managerLogin"];
    }

    public static bool ValidateRole(string roleStr)
    {
        if ((HttpContext.Current.Session["RoleList"] == null) || (HttpContext.Current.Session["IsAdmin"] == null))
        {
            Out();
            return false;
        }
        string str = HttpContext.Current.Session["RoleList"].ToString();
        string str2 = HttpContext.Current.Session["IsAdmin"].ToString();
        if (!str.Contains(roleStr) && !(str2 == "1"))
        {
            return false;
        }
        return true;
    }
}

